package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.CourseDOMapper;
import com.livepan.server.dao.my.MyCourseMapper;
import com.livepan.server.dataobject.CourseDO;
import com.livepan.server.dataobject.CourseDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.CourseModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.nio.cs.CharsetMapping;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseService {
    @Autowired
    private CourseDOMapper courseDOMapper;

    @Autowired
    private ValidatorImpl validator;

    @Autowired
    private MyCourseMapper myCourseMapper;


    @Autowired
    private CourseCategoryService courseCategoryService;


    private static final Logger LOG = LoggerFactory.getLogger(CourseService.class);

    public void list(PageModel pageModel) {
        PageHelper.startPage(pageModel.getPage(), pageModel.getSize());
        CourseDOExample courseDOExample = new CourseDOExample();
        List<CourseDO> courseDOList = courseDOMapper.selectByExample(courseDOExample);
        PageInfo<CourseDO> pageInfo = new PageInfo<>(courseDOList);
        pageModel.setTotal(pageInfo.getTotal());
        List<CourseModel> courseModelList = courseDOList.stream().map(
                this::convertCourseModelFromCourseDO
        ).collect(Collectors.toList());
        pageModel.setList(courseModelList);
    }

    public void save(CourseModel courseModel) throws BusinessException {
        ValidationResult validateResult = validator.validate(courseModel);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        CourseDO courseDO = CopyUtil.copy(courseModel, CourseDO.class);
        if (courseDOMapper.selectByPrimaryKey(courseDO.getId()) == null) {
            this.insert(courseDO);
        } else {
            this.update(courseDO);
        }

        //保存分类信息
        courseCategoryService.saveBatch(courseDO.getId(), courseModel.getCategoryModelList());
    }

    private void insert(CourseDO courseDO) {
        courseDO.setId(UUIDUtil.getShortUUID());
        courseDOMapper.insert(courseDO);
    }

    private void update(CourseDO courseDO) {
        courseDOMapper.updateByPrimaryKey(courseDO);
    }

    public void delete(String id) {
        courseDOMapper.deleteByPrimaryKey(id);
    }


    private CourseModel convertCourseModelFromCourseDO(CourseDO courseDO) {
        if (courseDO == null) {
            return null;
        }
        CourseModel courseModel = new CourseModel();
        BeanUtils.copyProperties(courseDO, courseModel);
        return courseModel;
    }

    int updateTime(String courseId) {
        LOG.info("更新课程时长：" + courseId);
        return myCourseMapper.updateTime(courseId);
    }
}
